<?php
/**
* @author Oliver Leisalu
* @package DomDateTime
*/
class DomAr_DateTime extends DomDateTime {
	
	private $parent;
	
	private $property;
	
	public function set( $stamp ) {
		parent::set( $stamp );
		if( $this->parent )
			$this->parent->setChanged( $this->propertyName );
	}
	
	public function setParent( DomArObject $parent, DomArProperty $property ) {
		$this->parent = $parent;
		$this->property = $property;
	}
}

/**
* @author Oliver Leisalu
* @package DomDateTime
*/
class DomArProperty_DateTime extends DomArProperty implements IDomArProperty, IDomArPropertyValidator, IDomArPropertyLoadListener, IDomArPropertyAlterTableListener,IDomArPropertyInsertListener,IDomArPropertyUpdateListener,IDomArPropertyBeforeSetListener {
	
	protected $defaultValue;
	
	protected $null;
	
	protected $index;
	
	protected $dbField;
	
	public function __construct( $name, $dbField = 'datetime', $defaultValue = NULL, $null = true, $index = NULL) {
		parent::__construct( $name );
		$this->defaultValue = $defaultValue;
		$this->null = $null;
		$this->index = $index;
		$this->dbField = $dbField;
	}
	
	public function init( DomArObject $object, array &$values ) {
		return (string)$values[$this->name];
	}
	
	private function getNewDateTimeObject() {
		$time = new DomAr_DateTime();
		$time->setDefaultFormat( $this->dbField == 'datetime' ? 'Y-m-d H:i:s' : 'Y-m-d' );
		return $time;
	}
	
	public function load( DomArObject $object, &$propertyValue ) {
		$d = $this->getNewDateTimeObject();
		$d->set( ($propertyValue !== NULL ? $propertyValue : ($this->defaultValue ? $this->defaultValue : NULL)) );
		$d->setParent( $object, $this );
		return $d;
	}
	
	public function beforeSet( DomArObject $object, &$oldPropertyValue, &$newPropertyValue ) {
		if( is_object($newPropertyValue) ) {
			$newPropertyValue->setParent( $object, $this );
		} else {
			$ob = $this->getNewDateTimeObject();
			$ob->set( $newPropertyValue );
			$ob->setParent( $object, $this );
			$newPropertyValue = $ob;
		}
	}
	
	public function isLoaded( DomArObject $object, &$propertyValue ) {
		return is_object($propertyValue);
	}
	
	public function validate( DomArObject $object, &$propertyValue, array &$customParams = array() ) {
		if( is_string( $propertyValue ) )
			$st = strtotime( $propertyValue );
		return (
				$propertyValue instanceof DomDateTime or 
				$st !== false or 
				is_numeric($propertyValue)
			) ? 0 : 1;
	}
	
	public function getFormat() {
		if( $this->dbField == 'date') {
			return 'Y-m-d';
		} else if( $this->dbField == 'datetime' ) {
			return 'Y-m-d H:i:s';
		} else if( $this->dbField == 'year' ) {
			return 'Y';
		} else if( $this->dbField == 'time' ) {
			return 'H:i:s';
		}
	}
	
	public function insert( DomArObject $object, &$propertyValue, DomArValueSet $set  ) {
		$this->update( $object, $propertyValue, $set );
	}
	
	public function update( DomArObject $object, &$propertyValue, DomArValueSet $set ) {
		$value = $object->{$this->name}->isNull() ? ($this->null ? 'NULL' : '\'\'') : '\''.$propertyValue->date( $this->getFormat() ).'\'';
		$set->set( $this->name, $value );
	}
	
	public function alterTable( DomArTable $table ) {
		return $table->changeColumn( $this->name, $this->dbField, $this->null );
	}
}
?>